<%@page import="org.lzy.pt.bean.Server"%>
<%@page import="org.apache.commons.fileupload.FileItemFactory"%>
<%@page import="org.apache.commons.fileupload.FileItem"%>
<%@page import="java.util.List"%>
<%@page import="org.apache.commons.fileupload.disk.DiskFileItemFactory"%>
<%@page import="org.apache.commons.fileupload.servlet.ServletFileUpload"%>
<%@page import="org.lzy.pt.ServerListManager"%>
<%@ page import="org.jivesoftware.util.*,java.net.URLEncoder,gnu.inet.encoding.Stringprep,gnu.inet.encoding.StringprepException"
    errorPage="error.jsp"
%>
<%@ page import="java.util.Map"%>
<%@ page import="java.util.HashMap"%>

<%@ taglib uri="http://java.sun.com/jstl/core_rt" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jstl/fmt_rt" prefix="fmt" %>

<%
	
	boolean another=false,create=false ,cancel=false;
	String T_DomainName = null;
	String T_Port = null;
	String T_Pwd = null ;
	byte[] T_PublicKey = null;
	Map<String, String> errors = new HashMap<String, String>();
	
	boolean isMultipart = ServletFileUpload.isMultipartContent(request);
	if(isMultipart){
		DiskFileItemFactory factory = new DiskFileItemFactory();
		ServletFileUpload upload = new ServletFileUpload(factory);
		List items = upload.parseRequest(request);
		
		for(Object item : items){
			FileItem fileItem = (FileItem)item;
			if(fileItem.isFormField()){
				if(fileItem.getFieldName().equals("T_DomainName")){
					T_DomainName = fileItem.getString();
				}
				if(fileItem.getFieldName().equals("T_Port")){
					T_Port = fileItem.getString();
				}
				if(fileItem.getFieldName().equals("T_Pwd")){
					T_Pwd = fileItem.getString();
				}
				if(fileItem.getFieldName().equals("create")){
					create = true;
				}
				if(fileItem.getFieldName().equals("another")){
					another = true;
					create = true;
				}
				if(fileItem.getFieldName().equals("cancel")){
					cancel = true;
				}
				
			}
			
			if(fileItem.getFieldName().equals("T_PublicKey")){
				T_PublicKey = fileItem.get();
			}
			
		}
		if (cancel) {
			response.sendRedirect("server-summary.jsp");
			return;
		}
		
		if (create) {
			if (errors.size() == 0) {
				try {

					ServerListManager.getInstance().addServer(T_DomainName, Integer.parseInt(T_Port), T_PublicKey, T_Pwd);
					
					if (another) {
						response.sendRedirect("server-create.jsp?success=true");
					} else {
						response.sendRedirect("server-summary.jsp");
					}
					return;
				} catch (Exception e) {
					errors.put("general", "");
					Log.error(e);
				}
			}
		}
		
	}
	
%>

<html>
    <head>
        <title><fmt:message key="server.create.title"/></title>
        <meta name="pageID" content="server-create"/>
    </head>
    <body>

<p><fmt:message key="server.create.info" /></p>

<%
	if (!errors.isEmpty()) {
%>

    <div class="jive-error">
    <table cellpadding="0" cellspacing="0" border="0">
    <tbody>
        <tr>
            <td class="jive-icon"><img src="images/error-16x16.gif" width="16" height="16" border="0" alt=""/></td>
            <td class="jive-icon-label">

            <%
            	if (errors.get("general") != null) {
            %>
                <fmt:message key="server.create.error_creating_server" />
            <%
            	} else {
            %>
                <fmt:message key="error.unknown" />
            <%
            	}
            %>
            </td>
        </tr>
    </tbody>
    </table>
    </div>
    <br>

<%
	} else if (request.getParameter("success") != null) {
%>

    <div class="jive-success">
    <table cellpadding="0" cellspacing="0" border="0">
    <tbody>
        <tr><td class="jive-icon"><img src="images/success-16x16.gif" width="16" height="16" border="0" alt=""></td>
        <td class="jive-icon-label">
        <fmt:message key="server.create.created_success" />
        </td></tr>
    </tbody>
    </table>
    </div><br>

<%
	}
%>

<form name="f" action="server-create.jsp" method="post" enctype="multipart/form-data">

	<div class="jive-contentBoxHeader">
		<fmt:message key="server.create.new_server" />
	</div>
	<div class="jive-contentBox">
		<table cellpadding="3" cellspacing="0" border="0">
		<tbody>
		<tr>
			<td width="1%" nowrap><label for="T_DomainName"><fmt:message key="server.T_DomainName" />:</label> *</td>
			<td width="99%">
				<input type="text" name="T_DomainName" size="30" maxlength="75" id="T_DomainName">
			</td>
		</tr>
		<tr>
			<td nowrap><label for="T_Port"><fmt:message key="server.T_Port" />:</label>*</td>
			<td width="99%">
				<input type="text" name="T_Port" size="30" maxlength="75" value="<%=((T_Port != null) ? T_Port : "")%>"
				 id="T_Port">
			</td>
		</tr>
		<tr>
			<td nowrap><label for="T_PublicKey"><fmt:message key="server.T_PublicKey" />:</label></td>
			<td width="99%">
				<input type="file" name="T_PublicKey" width="100px;"/>
<%-- 				<input type="text" name="T_PublicKey" size="30" maxlength="75" value="<%= ((T_PublicKey!=null) ? T_PublicKey : "") %>" --%>
<!-- 				 id="T_PublicKey"> -->
			</td>
		</tr>
		<tr>
			<td nowrap><label for="T_Pwd"><fmt:message key="server.T_Pwd" />:</label></td>
			<td width="99%">
				<input type="text" name="T_Pwd" size="30" maxlength="75" value="<%=((T_Pwd != null) ? T_Pwd : "")%>"
				 id="T_Pwd">
			</td>
		</tr>
        <tr>
			<td colspan="2" style="padding-top: 10px;">
				<input type="submit" name="create" value="<fmt:message key="server.create.create" />">
				<input type="submit" name="another" value="<fmt:message key="server.create.create_another" />">
				<input type="submit" name="cancel" value="<fmt:message key="global.cancel" />"></td>
		</tr>
		</tbody>
		</table>

	</div>

	<span class="jive-description">
    * <fmt:message key="user.create.requied" />
    </span>
</form>

<script language="JavaScript" type="text/javascript">
document.f.username.focus();
Calendar.setup({
    inputField : "T_ExecDateTime_show",
    trigger    : "T_ExecDateTime_show",
    onSelect   : function() { 
    	this.hide();
    	document.getElementById("T_ExecDateTime").value = this.selection.get().toString()+this.getTime();
    },
    showTime   : 12,
    dateFormat : "%Y-%m-%d %I:%M %p"
  });
</script>
    </body>
</html>